package com.qing.gui.frame;

import java.awt.*;

/**
 * 实现简易计算器
 */
public class TestCalc {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame("文本输入框监听", 200, 200, 200, 200, true, Color.pink);
        //限制字符数10个
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        Label label = new Label("+");
        Button button = new Button("=");
        TextField numResult = new TextField(10);
        myFrame.add(num1);
        myFrame.add(label);
        myFrame.add(num2);
        myFrame.add(button);
        myFrame.add(numResult);
        //设置流式布局，从左到右
        myFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
        //调整最优布局
        myFrame.pack();
        //给button等于号按钮设置监听事件
        button.addActionListener(e -> {
            //获取两个文本框中的值
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            //计算两个值的和并赋值给结果文本
            numResult.setText(String.valueOf((n1 + n2)));
        });

    }
}
